function addList(list) {
    $("#ppp").html("")
    $("#ppp").append("<li><input type='checkbox' onchange='allChange()' id='allchecks' /></li>")
    for (var i = 0; i < list.length; i++) {
        $("#ppp").append($('<li id="li_' + list[i].goodsId + '" class="minicart-item">\n' +
            '<input style="margin: 36px 0px;" onchange="goodsChange()" type="checkbox" name="goods" value="'+list[i].goodsId+'" />'+
            '                                <div class="minicart-thumb">\n' +
            '                                <a href="goodsDetails?gid=' + list[i].goodsId + '">\n' +
            '                                <img src="'+list[i].goodsImage+'" alt="product">\n' +
            '                                </a>\n' +
            '                                </div>\n' +
            '                                <div class="minicart-content">\n' +
            '                                <h3 class="product-name">\n' +
            '                                <a href="goodsDetails?gid=' + list[i].goodsId + '">' + list[i].goodsName + '</a>\n' +
            '                            </h3>\n' +
            '                            <p>\n' +
            '                            <span class="cart-quantity"><span onclick="subShop('+list[i].goodsId+')">-</span>' + list[i].goodsNumber + '<span onclick="addShop('+list[i].goodsId+')">+</span><strong>&times;</strong></span>\n' +
            '                            <span class="cart-price">' + list[i].goodsPrice + '</span>\n' +
            '                            </p>\n' +
            '                            </div>\n' +
            '                            <button class="minicart-remove" style="margin: 30px -20px;"><i onclick="remo(' + list[i].goodsId + ')" class="lnr lnr-cross"></i></button>\n' +
            '                            </li>'))
    }
}

function strongValue(str){
    $(".notification").eq(0).html(str[0])
    $("#jq strong").eq(0).html("￥ "+str[2])
    $("#jq strong").eq(1).html("￥ "+(str[1]-str[2]))
    $("#jq strong").eq(2).html("￥ "+str[1])
}
    $(document).ready(function () {
        $.ajax({
            url: "car",
            type: "post",
            success: function (result) {
                if (result.result == "0000") {
                    var str = result.resultMsg.split(",");
                    strongValue(str)
                    var list = result.list;
                    addList(list)
                }
            },
            dataType: "json"
        })
    })

    function addShop(gid) {
        $.ajax({
            url: "addShop",
            type: "post",
            data: {"gid": gid},
            success: function (result) {
                if (result.result == "0000") {
                    console.info(result)
                    var str = result.resultMsg.split(",");
                    strongValue(str)
                    var list = result.list;
                    addList(list)
                } else {

                }
            },
            dataType: "json"
        })
    }

    function subShop(gid) {
        $.ajax({
            url: "subShop",
            type: "post",
            data: {"gid": gid},
            success: function (result) {
                if (result.result == "0000") {
                    var str = result.resultMsg.split(",");
                    strongValue(str)
                    var list = result.list;
                    addList(list)
                } else {

                }
            },
            dataType: "json"
        })
    }
    function remo(gid) {
        $.ajax({
            url: "removeCar",
            type: "post",
            data: {"gid": gid},
            success: function (result) {
                if (result.result == "0000") {
                    var s = "#li_" + gid;
                    $(s).remove()
                    var str = result.resultMsg.split(",");
                    strongValue(str)
                }
            },
            dataType: "json"
        })
    }
    function addCarss(gid) {
        $.ajax({
            url: "addCar",
            type: "post",
            data: {"gid": gid,"num":$(":input[name='goodsNum']").val()},
            success: function (result) {
                if (result.result == "0000") {
                    var str = result.resultMsg.split(",");
                    strongValue(str)
                    var list = result.list;
                    addList(list)
                    $(":input[name='goodsNum']").val("0")
                }
            },
            dataType: "json"
        })
    }
    function checkout() {
        if($("#ppp").html() == ""){
            alert("购物车中无商品!")
        }else if($("input[name='goods']:checked").length==0){
            alert("请选择要结算的商品!")
        }else{
            var goods = "";
            for (var i = 0; i < $("input[name='goods']:checked").length;i++){
                goods+=$("input[name='goods']:checked").eq(i).val()+",";
                if(i=$("input[name='goods']:checked").length-1){
                    goods+=$("input[name='goods']:checked").eq(i).val();
                }
            }
            location.href = "checkout1?goods="+goods;
            console.info(goods)
        }
    }
function allChange(){
    $('input[name="goods"]').prop('checked',$('#allchecks').prop('checked'));
}
function goodsChange() {
    if( $('input[name="goods"]').length == $('input[name="goods"]:checked').length){
        $('#allchecks').prop('checked',true)
    }else{
        $('#allchecks').prop('checked',false)
    }
}